package cn.elwy.rcp.ui.table.util;

import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TreeColumn;

public class ColumnVisibleUtil {

  public static String DEFAULT_COLUMN_WIDTH = "_default_column_width_";
  public static String COLUMN_WIDTH = "_column_width_";

  public static void setColumnVisible(TableColumn column, boolean visible) {
    if (visible) {
      column.setResizable(true);
      Integer width = (Integer) column.getData(COLUMN_WIDTH);
      if (width != null) {
        column.setWidth(width);
      }
    } else {
      if (column.getResizable() == false) {
        return;
      }
      column.setData(COLUMN_WIDTH, column.getWidth());
      column.setWidth(0);
      column.setResizable(false);
    }
  }

  public static void setColumnVisible(TreeColumn column, boolean visible) {
    if (visible) {
      column.setResizable(true);
      Integer width = (Integer) column.getData(COLUMN_WIDTH);
      if (width != null) {
        column.setWidth(width);
      }
    } else {
      if (column.getResizable() == false) {
        return;
      }
      column.setData(COLUMN_WIDTH, column.getWidth());
      column.setWidth(0);
      column.setResizable(false);
    }
  }
}
